package Notepad;


import java.util.Scanner;

public class SecureNotepad extends SimpleNotepad {
	Scanner sc=new Scanner(System.in);
	private static final String SMALL_LETTERS_CHECK = ".*[a-z].*";
	private static final String CAPITAL_LETTERS_CHECK = ".*[A-Z].*";
	// ".*\\d.*"
	private static final String NUMBERS_CHECK = ".*\\d.*";
	String password;
	private Scanner input = new Scanner(System.in);

	SecureNotepad(String pass) {
		super();
		if (pass != null)
			this.password = initPassword();;
	}
	private String initPassword() {
		String password = "";
		System.out.println("enter password");
		password = sc.next();
		if (!isPasswordCorrect(password)) {
			System.out.println("Password should contain upper and lower case letters and numbers.");
		}

		return password;
	}
	@Override
	public void addPageText(int pageNumber, String text) {
		if (getPassword().equals(this.password))
			super.addPageText(pageNumber, text);
		else
			System.out.println("Password did not match");
	}

	@Override
	public void removePageText(int pageNumber) {
		if (getPassword().equals(this.password))
			super.removePageText(pageNumber);
		else
			System.out.println("Password did not match");
	}

	protected String getPassword() {
		System.out
				.println("Enter password since you are using secured notepad : ");
		return input.nextLine();
	}

	private boolean isPasswordCorrect(String password) {
		return !(password.length() < 5 || !password.matches(NUMBERS_CHECK)
				|| !password.matches(CAPITAL_LETTERS_CHECK) || !password
					.matches(SMALL_LETTERS_CHECK));
	}
}
